import cv2
import mediapipe as mp
import time
import numpy as np

mpHands = mp.solutions.hands
hands = mpHands.Hands()
mpDraw = mp.solutions.drawing_utils

def distance(lmList):
    return np.sqrt((lmList[8][1] - lmList[12][1])**2 +(lmList[8][2]-lmList[12][2])**2 )

pTime = 0
cTime = 0

cap = cv2.VideoCapture(0)

while cap.isOpened():
    sucess,img = cap.read()
    imgRGB = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
    results = hands.process(imgRGB)

    lmList = []

    if results.multi_hand_landmarks:
        for handLms in results.multi_hand_landmarks:
            for id,lm in enumerate(handLms.landmark):
                h,w,c = img.shape
                cx,cy = int(lm.x * w), int(lm.y * h)
                lmList.append([id,cx,cy])
            mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)
    #print(lmList)
    cTime = time.time()
    fps = 1/(cTime - pTime)
    pTime = cTime
    cv2.putText(img, str(int(fps)), (10,70), cv2.FONT_HERSHEY_PLAIN, 3, (255,0,255),1)
    if(lmList):
        print(distance(lmList))
    if cv2.waitKey(1) == ord('q'):
        break
    cv2.imshow("video",img)

cap.release()
cv2.destroyAllWindows()
